<template>
    <div>
        <span style="font-size: 25px;margin-left: 50px"><b>基本信息</b></span>
        <hr style="border: 1px dashed darkgray;width: 92%;transform: scaleY(0.5);margin-bottom: 25px;margin-left: 50px">
        <el-form :model="caseForm" ref="caseForm" :rules="rules1" label-width="100px" class="demo-caseForm" onsubmit="return false">
            <el-row>
                <el-col :span="10" :offset=1>
                    <el-form-item label="服务时间" prop="collectionTime">
                        <el-date-picker type="date" placeholder="选择服务时间" v-model="caseForm.collectionTime " style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset=1>
                    <el-form-item label="服务费用" prop="caseAgencyfee">
                        <el-input v-model.number="caseForm.caseAgencyfee" placeholder="请填写服务费用" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset=1>
                    <el-form-item label="服务内容" prop="caseCause">
                        <el-input v-model="caseForm.caseCause" placeholder="请填写服务内容" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="10"  :offset=1>
                    <el-form-item label="服务人次" prop="caseFilenumber">
                        <el-input v-model.number="caseForm.caseFilenumber" placeholder="请填写服务人次" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="23" :offset=1>
                    <el-form-item label="服务类型" prop="caseCaseTypeId">
                        <el-radio-group v-model="caseForm.caseCaseTypeId" style="margin-top: 11px;">
                            <span v-for="(item,index) in caseType" :key="index" style="margin-left: 20px">
                            <el-radio :label="item.id">{{item.caseTypeName}}</el-radio>
                            </span>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="3"><span style="font-size: 25px;margin-left: 50px"><b>其他选填</b></span></el-col>
                <el-col :span="1" style="padding-top: 5px">
                    <el-switch v-model="xt"></el-switch>
                </el-col>
            </el-row>
            <hr style="border: 1px dashed darkgray;width: 1360px;transform: scaleY(0.5);margin-bottom: 25px">
            <div v-if="xt">
                <el-row>
                    <el-col :span="10" :offset=1>
                        <el-form-item label="委托人" prop="caseWtr">
                            <el-input v-model="caseForm.caseWtr" placeholder="请填写委托人"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10"  :offset=1>
                        <el-form-item label="对方当事人" prop="caseOppositeParties">
                            <el-input v-model="caseForm.caseOppositeParties" placeholder="请填写对方当事人"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="21" :offset=1>
                        <el-form-item label="相关文件" prop="caseCorrelationFile">
                            <el-button type="primary" @click="fileUp">上传文件<i class="el-icon-upload el-icon--right"></i></el-button>
                            <el-upload
                                    class="upload-demo"
                                    ref="upload"
                                    action=""
                                    :limit="1"
                                    :http-request="submitUpload"
                                    :before-upload="beforeUpload"
                                    :on-exceed="onExceed"
                                    v-show="false">
                                <el-button ref="xzwj">选取文件</el-button>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="21" :offset=1>
                        <el-form-item label="备注" prop="caseRemarks">
                            <el-input type="textarea" v-model="caseForm.caseRemarks"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
        </el-form>
        <span style="font-size: 25px;margin-left: 50px"><b>办案律师</b></span>
        <hr style="border: 1px dashed darkgray;width: 1360px;transform: scaleY(0.5);margin-bottom: 25px">
        <el-form :model="cwForm" ref="cwForm" :rules="rules2" label-width="100px" class="demo-cwForm" onsubmit="return false">
            <el-row>
                <el-col :span="5">
                    <el-form-item prop="caseAttorney">
                        <el-input v-model="cwForm.caseAttorney" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5" style="margin-left: -99px">
                    <el-form-item prop="zhuBanLvShi">
                        <el-input v-model="cwForm.zhuBanLvShi" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5" :offset=1>
                    <el-form-item label="业绩分成" prop="cwPerformancesplit">
                        <el-input v-model.number="cwForm.cwPerformancesplit" placeholder="请输入业绩分成"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5" style="margin-left: -99px">
                    <el-form-item prop="cwSplitType">
                        <el-select v-model="cwForm.cwSplitType" style="width: 200px">
                            <el-option value="百分比">百分比</el-option>
                            <el-option value="固定金额">固定金额</el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3" :offset=1>
                    <el-button @click="addRows" type="primary">增加一行</el-button>
                </el-col>
            </el-row>
            <el-row v-for="(item,index) in cwForm.rows" :key="index">
                <el-col :span="5">
                    <el-form-item :rules="rules2.caseAttorney" :prop="'rows.'+index+'.caseAttorney'">
                        <el-select v-model="item.caseAttorney" style="width: 205px">
                            <el-option value="督办律师">督办律师</el-option>
                            <el-option value="协助律师">协助律师</el-option>
                            <el-option value="实习律师">实习律师</el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="5" style="margin-left: -99px">
                    <el-form-item :rules="rules2.zhuBanLvShi" :prop="'rows.'+index+'.zhuBanLvShi'">
                        <el-input v-model="item.zhuBanLvShi" readonly="readonly" placeholder="点击选择律师" @click.native="choiceLawyersDialog(index)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5" :offset=1>
                    <el-form-item label="业绩分成" :rules="rules2.cwPerformancesplit" :prop="'rows.'+index+'.cwPerformancesplit'">
                        <el-input v-model.number="item.cwPerformancesplit" placeholder="请输入业绩分成"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5" style="margin-left: -99px">
                    <el-form-item :rules="rules2.cwSplitType" :prop="'rows.'+index+'.cwSplitType'">
                        <el-select v-model="item.cwSplitType" style="width: 200px">
                            <el-option value="百分比">百分比</el-option>
                            <el-option value="固定金额">固定金额</el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3" :offset=1>
                    <el-button @click="reRows(index)" type="danger">删除</el-button>
                </el-col>
            </el-row>
            <el-form-item>
                <el-row>
                    <el-col :span="1" :offset=5>
                        <el-button type="info" @click="back">返回列表</el-button>
                    </el-col>
                    <el-col :span="1" :offset=2>
                        <el-button type="primary" @click="clientFormSubmit('caseForm')">立即创建</el-button>
                    </el-col>
                    <el-col :span="1" :offset=2>
                        <el-button @click="resetForm('caseForm')">重置</el-button>
                    </el-col>
                </el-row>
            </el-form-item>
        </el-form>
        <el-dialog
                title="请选择办案律师"
                :visible.sync="getLawyersList"
                width="50%">
            <!--                    :before-close="handleClose"-->
            <span>
                <el-table
                        @row-click="rowClick"
                        :data="LawyersList"
                        border
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="empName"
                            label="姓名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="empSex"
                            label="性别"
                            :formatter="sexFormatter">
                    </el-table-column>
                    <el-table-column
                            prop="empPosition"
                            label="职务">
                    </el-table-column>
                    <el-table-column
                            prop="empPhone"
                            label="电话">
                    </el-table-column>
                </el-table>
            </span>
            <span slot="footer" class="dialog-footer">
<!--                    <el-button @click="getLawyersList = false">取 消</el-button>-->
                <el-button type="primary" @click="getLawyersList = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import AddCaseService from "../../model/AddCaseService";

    const addCaseService=AddCaseService.getInstance();
    export default {
        name: "AddCaseAgency",
        props:[
            'caseId',
            'addCase'
        ],
        data(){
            return{
                LawyersList:[],//办案律师列表
                getLawyersList:false,//查询律师列表对话框
                files:'',//文件
                fileName:'',//文件名
                zhuBanLvShiIndex:"",//主办律师下标
                xt:false,//switch 滑块
                caseType:[],//案件类型


                caseForm:{//案件表单
                    caseLawId:"",//案号id
                    caseNo:"",//案号
                    caseCause:"",//案由
                    caseWtr:"",//委托人
                    collectionTime:"",//收案日期
                    caseOppositeParties:"",//对方当事人
                    caseAgencyfee:"",//代理费
                    caseUnpaidsal:"",//未付金额
                    caseFilenumber:"",//服务人次
                    caseCorrelationFile:"",//相关文件
                    caseRemarks:"",//备注
                    caseCaseTypeId:"",//案件类型id
                },
                rules1:{//案件表单验证规则
                    caseFilenumber:[
                        {required:true,message:"请填写服务人次!",trigger:"blur"},
                        {type:"number",message: "请正确填写服务人次!(数字类型)"}
                    ],
                    caseCause:[
                        {required:true,message:"请填写服务内容!",trigger:"blur"}
                    ],
                    collectionTime:[
                        {required:true,message:"请选择服务日期!",trigger:"blur"}
                    ],
                    caseAgencyfee:[
                        {required:true,message:"请填写服务费!",trigger:"blur"},
                        {type:"number",message: "请正确填写服务费!(数字类型)"}
                    ],
                    caseCaseTypeId:[
                        {required:true,message:"请选择服务类型!",trigger:"blur"}
                    ],
                },


                cwForm:{//办案律师分成表单
                    caseAttorney:"主办律师",//主办律师
                    hrEmpId:JSON.parse(localStorage["user"])[0].hrEmpId,//主办律师id
                    zhuBanLvShi:JSON.parse(localStorage["user"])[0].userRealname,//主办律师
                    cwPerformancesplit:"",//百分比
                    cwSplitType:"",//模式
                    rows:[],//添加一行
                },
                rules2:{//办案律师分成表单验证规则
                    caseAttorney:[
                        {required:true,message:"请选择律师类型!",trigger:"blur"}
                    ],
                    zhuBanLvShi:[
                        {required:true,message:"请选择律师!",trigger:"blur"}
                    ],
                    cwPerformancesplit:[
                        {type:"number",required:true,message:"业绩分成应为数字类型!",trigger:"blur"}
                    ],
                    cwSplitType:[
                        {required:true,message:"请选择分成模式!",trigger:"blur"}
                    ]
                },
            }
        },
        created() {
            this.init()
        },
        methods:{
            rowClick(row){//办案律师表格点击事件//, column, event
                this.cwForm.rows[this.zhuBanLvShiIndex].hrEmpId=row.id;
                this.cwForm.rows[this.zhuBanLvShiIndex].zhuBanLvShi=row.empName;
                this.getLawyersList=false;
            },
            reRows(index){//删除一行
                this.cwForm.rows.splice(index, 1)
            },
            addRows(){//添加一行
                const rows={
                    caseAttorney:"",//律师
                    caseLawId:"",//案件id
                    hrEmpId:"",//律师id
                    zhuBanLvShi:"",//律师
                    cwPerformancesplit:"",//业绩分成
                    cwSplitType:"",//分成模式
                };
                this.cwForm.rows.push(rows)
            },
            sexFormatter(row, column, cellValue){//性别转换
                if(cellValue==="M"){
                    return "男"
                }else{
                    return "女"
                }
            },
            resetForm(formName) {//重置表单
                this.$refs[formName].resetFields();
            },
            fileUp(){//文件上传点击事件
                this.$refs.xzwj.$el.click()
            },
            onExceed(){//文件个数超出限制时
                this.$message({
                    type:"warning",
                    message:"本次文件上传已达限制"
                })
            },
            beforeUpload(file){//文件上传之前
                this.files=file;
                this.fileName=file.name;
            },
            submitUpload() {//文件上传提交事件
                if (this.fileName === "") {
                    this.$message.warning('请选择要上传的文件！');
                    return false
                }
                let fileFormData = new FormData();
                fileFormData.append('file', this.files, this.fileName);//file是键，files是值，就是要传的文件，fileName是要传的文件名
                let requestConfig = {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    },
                };
                addCaseService.fileUp(fileFormData,requestConfig,this.$http).then(msg=>{
                    this.caseForm.caseCorrelationFile=msg.data.data;
                    this.$message({
                        type:"success",
                        message:"上传成功~"
                    })
                })
            },
            choiceLawyersDialog(index){//选择办案律师对话框
                this.zhuBanLvShiIndex=index;
                this.getLawyersList=true;
            },
            clientFormSubmit(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid){
                        this.$refs.cwForm.validate((valid) => {
                            if(valid){
                                this.caseForm.caseUnpaidsal=this.caseForm.caseAgencyfee;
                                if(this.caseId===""){
                                    this.caseForm.collectionTime=this.$date.formatDate(this.caseForm.collectionTime, "yyyy-MM-DD");
                                    let formData = new FormData();
                                    for(const key in this.caseForm){
                                        if(Object.prototype.hasOwnProperty.call(this.caseForm, key)){
                                            if(this.caseForm[key]!=null){
                                                formData.append(key,this.caseForm[key]);
                                            }
                                        }
                                    }
                                    const date = this.$date.formatDate(new Date(), "yyyy-MM-DD hh:mm:ss");//yyyy-MM-DD hh:mm:ss
                                    formData.append("caseSubtime",date);
                                    formData.append("caseSubmitter",JSON.parse(localStorage["user"])[0].userRealname);
                                    formData.append("hrEmpId",JSON.parse(localStorage["user"])[0].hrEmpId);
                                    addCaseService.addCaseLaw(this.$http,formData).then(msg=>{
                                        if(msg.data.status==="50000"){
                                            this.$message({
                                                type:"error",
                                                message:"出错了呢~"
                                            })
                                        }else {
                                            this.caseForm.caseLawId=msg.data.data;
                                            const performancesplit={
                                                caseAttorney:this.cwForm.caseAttorney,//主办律师
                                                caseLawId:msg.data.data,//案件id
                                                hrEmpId:JSON.parse(localStorage["user"])[0].hrEmpId,//主办律师id
                                                zhuBanLvShi:this.cwForm.zhuBanLvShi,//主办律师
                                                cwPerformancesplit:this.cwForm.cwPerformancesplit,//业绩分成
                                                cwSplitType:this.cwForm.cwSplitType,//分成模式
                                            };
                                            let rows=[];
                                            rows.push(performancesplit);
                                            for (let i = 0; i < this.cwForm.rows.length; i++) {
                                                this.cwForm.rows[i].caseLawId=msg.data.data;
                                                rows.push(this.cwForm.rows[i])
                                            }
                                            addCaseService.addcwPerformancesplit(this.$http,rows).then(msg=>{
                                                if(msg.data.status==="50000"){
                                                    this.$message({
                                                        type:"error",
                                                        message:"出错了呢~"
                                                    })
                                                }else{
                                                    addCaseService.upCaseAt(this.$http,this.caseForm.caseLawId);
                                                    this.$message({
                                                        type: "success",
                                                        message: "立案成功~"
                                                    });
                                                    if(this.addCase==="1"){
                                                        this.$router.push("/home/caseAgency")
                                                    }else{
                                                        this.back();
                                                    }
                                                }
                                            });
                                        }
                                    })
                                }else{
                                    let formData = new URLSearchParams();
                                    for(const key in this.caseForm){
                                        if(Object.prototype.hasOwnProperty.call(this.caseForm, key)){
                                            if(this.caseForm[key]!=null){
                                                formData.append(key,this.caseForm[key]);
                                            }
                                        }
                                    }
                                    formData.append("id",this.caseId);
                                    formData.append("type",2);
                                    formData.append("caseApproveStatus",1);
                                    addCaseService.upCase(this.$http,formData).then(msg=>{
                                        if(msg.data.status==="50000"){
                                            this.$message({
                                                type:"error",
                                                message:"出错了呢~"
                                            })
                                        }else {
                                            addCaseService.deCwPerformancesplit(this.$http,this.caseId);
                                            const performancesplit={
                                                caseAttorney:this.cwForm.caseAttorney,//主办律师
                                                caseLawId:this.caseId,//案件id
                                                hrEmpId:JSON.parse(localStorage["user"])[0].hrEmpId,//主办律师id
                                                zhuBanLvShi:this.cwForm.zhuBanLvShi,//主办律师
                                                cwPerformancesplit:this.cwForm.cwPerformancesplit,//业绩分成
                                                cwSplitType:this.cwForm.cwSplitType,//分成模式
                                            };
                                            let rows=[];
                                            rows.push(performancesplit);
                                            for (let i = 0; i < this.cwForm.rows.length; i++) {
                                                this.cwForm.rows[i].caseLawId=msg.data.data;
                                                rows.push(this.cwForm.rows[i])
                                            }
                                            addCaseService.addcwPerformancesplit(this.$http,rows).then(msg=>{
                                                if(msg.data.status==="50000"){
                                                    this.$message({
                                                        type:"error",
                                                        message:"出错了呢~"
                                                    })
                                                }else{
                                                    addCaseService.upCaseAt(this.$http,this.caseId);
                                                    this.$message({
                                                        type: "success",
                                                        message: "修改成功~"
                                                    });
                                                    this.back();
                                                }
                                            });
                                        }
                                    })
                                }
                            }
                        });
                    }
                });
            },
            back(){
                this.$emit("back");
            },
            init(){//初始化
                this.$loading({//懒加载
                    lock: true,
                    text: '正在查询...',
                    spinner: 'fa fa-refresh fa-spin fa-3x',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                addCaseService.queryHrEmp(this.$http).then(msg=>{
                    this.LawyersList=msg.data.data;
                });
                addCaseService.queryChildren(this.$http,"1011").then(msg=>{
                    this.caseType=msg.data.data.data
                });
                if(this.caseId===""){
                    this.caseForm.collectionTime=new Date();
                    addCaseService.queryCaseNo(this.$http).then(msg=>{//处理案号
                        let data;
                        if (msg.data.data==null){
                            data="1";
                        }else{
                            data =(parseInt(msg.data.data)+1)+"";
                        }
                        if(data.length===1){
                            data="2021(文)第000"+data+"号"
                        }else if(data.length===2){
                            data="2021(文)第00"+data+"号"
                        }else if(data.length===3){
                            data="2021(文)第0"+data+"号"
                        }else{
                            data="2021(文)第"+data+"号"
                        }
                        this.caseForm.caseNo=data;
                        this.$loading().close()
                    })
                }else{
                    const params={
                        id:this.caseId
                    };
                    addCaseService.querySenior(this.$http,params).then(msg=>{
                        const data=msg.data.data.list[0];
                        this.caseForm.caseNo=data.caseNo;
                        this.caseForm.collectionTime=data.collectionTime;
                        this.caseForm.caseAgencyfee=data.caseAgencyfee;
                        this.caseForm.caseCause=data.caseCause;
                        this.caseForm.caseFilenumber=parseInt(data.caseFilenumber);
                        this.caseForm.caseCaseTypeId=parseInt(data.caseCaseTypeId);
                        this.caseForm.caseWtr=data.caseWtr;
                        this.caseForm.caseOppositeParties=data.caseOppositeParties;
                        this.caseForm.caseRemarks=data.caseRemarks;
                        this.$loading().close()
                    });
                }
            }
        },
    }
</script>

<style scoped>

</style>